package com.anyisn.anyflickr.shell.view
{
	import com.adobe.webapis.flickr.PagedPhotoList;
	import com.adobe.webapis.flickr.Photo;
	import com.anyisn.anyflickr.common.ApplicationConstants;
	import com.anyisn.anyflickr.common.events.DownloadEvent;
	import com.anyisn.anyflickr.common.events.PhotoLoadEvent;
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.view.components.ThumbsTile;
	import com.anyisn.anyflickr.shell.view.components.itemrender.ThumbIR;
	import com.anyisn.anyflickr.shell.view.components.popup.PhotoSizes;
	import com.anyisn.flickr.as3.utils.Urls;
	import com.anyisn.puremvc.as3.multicore.patterns.mediator.AbstractPopupMediator;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Menu;
	import mx.core.IFlexDisplayObject;
	import mx.effects.Move;
	import mx.events.CloseEvent;
	import mx.events.EffectEvent;
	import mx.events.MenuEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class ThumbsTileMediator extends AbstractPopupMediator
	{
		public static const NAME:String = 'ThumbsTileMediator';
		
		public function ThumbsTileMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
			
			var menuData:Array = ApplicationConstants.photoMenu;
			thumbMenu = Menu.createMenu(null, menuData);
			thumbMenu.addEventListener(MenuEvent.ITEM_CLICK, clickMenuItem);
			thumbsTile.addEventListener(ThumbsTile.MENU_SHOW, showMenu);
			thumbsTile.addEventListener(PhotoLoadEvent.LOAD_PHOTO_SUCCESS, photoLoaded);
		}
		
		override public function listNotificationInterests():Array {
			return [ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS,
					ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS,
					ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS,
					ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS,
					
					ShellFacade.PHOTOS_GET_SIZES_SUCCESS
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS:
				case ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS:
				case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS:
				case ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS:
					var photoList:PagedPhotoList = notification.getBody() as PagedPhotoList;
					thumbsTile.initialThumbs(photoList);
					moveDown();
					break;
				case ShellFacade.PHOTOS_GET_SIZES_SUCCESS:
					var sizes:Array = notification.getBody() as Array;
					var ps:IFlexDisplayObject = PopUpManager.createPopUp(thumbsTile, PhotoSizes, true);
					var psTW:PhotoSizes = ps as PhotoSizes;
					psTW.sizes = sizes;
					psTW.addEventListener(PhotoSizes.DOWNLOAD, downloadPhoto);
					psTW.addEventListener(CloseEvent.CLOSE, removePopUp);
					PopUpManager.centerPopUp(ps);
					break;
			}
		}
		
		// sizes for download
		private function downloadPhoto(e:DownloadEvent):void {
			sendNotification(ShellFacade.PHOTO_DOWNLOAD, e.source);
			PopUpManager.removePopUp(e.target as PhotoSizes);
		}
		
		private function removePopUp(e:Event):void {
			PopUpManager.removePopUp(e.target as IFlexDisplayObject);
		}
		
		// move tile down and up
		private function moveDown():void {
			var moveDown:Move = new Move(thumbsTile);
			moveDown.yBy = thumbsTile.height;
			moveDown.duration = 800;
			moveDown.addEventListener(EffectEvent.EFFECT_END, tileMovedDown);
			moveDown.play();
		}
		
		private function tileMovedDown(e:EffectEvent):void {
			if(thumbsTile.numChildren > 0) {
				thumbsTile.clear();
			}
			while(thumbsTile.numChildren > 0) {
				trace(thumbsTile.numChildren);
				var thumb:ThumbIR = thumbsTile.getChildAt(0) as ThumbIR;
				thumb.clear();
				thumbsTile.removeChildAt(0);
				thumb = null;
			}
			moveUp();
		}
		
		private function moveUp():void {
			var moveDown:Move = new Move(thumbsTile);
			moveDown.yBy = -thumbsTile.height;
			moveDown.duration = 10;
			moveDown.addEventListener(EffectEvent.EFFECT_END, tileMovedUp);
			moveDown.play();
		}
		
		private function tileMovedUp(e:EffectEvent):void {
			thumbsTile.addThumbs();
		}
		
		// MENU_HANDLER
		private function showMenu(e:Event):void {
// - not download images that have the no download flag set
// - not download images that are All Rights Reserved unless the user has permission from the photo owner
			/*if(thumbsTile.selectedItem.license == 0 || thumbsTile.selectedItem.canDownload == 0) {
				data[1].enabled = false;
			}*/
// all images displayed via your application should link back to the Flickr photo pages
			/*var urls:Array = thumbsTile.selectedItem.urls;
			if(urls.length && urls[0].type == 'photopage') {
				data[3].enabled = true;
			} else {
				data[3].enabled = false;
			}*/
			var x:Number;
			var y:Number;
			if(thumbsTile.mouseX > thumbsTile.width*0.5) {
				x = thumbsTile.mouseX - thumbMenu.width;
			} else {
				x = thumbsTile.mouseX;
			}
			if(thumbsTile.mouseY > thumbsTile.height*0.5) {
				y = thumbsTile.mouseY - thumbMenu.height;
			} else {
				y = thumbsTile.mouseY;
			}
			thumbMenu.show(x, y);
		}
		
		private function clickMenuItem(e:MenuEvent):void {
			switch(e.item.name) {
				case ShellFacade.NAVIGATE_TO_PHOTOSTREAM:
					var rqt0:URLRequest = new URLRequest(Urls.getPhotostreamPage(thumbsTile.selectedItem.ownerId));
					navigateToURL(rqt0);
					break;
				case ShellFacade.NAVIGATE_TO_PHOTOPAGE:
					var rqt1:URLRequest = new URLRequest(Urls.getPhotoPage(thumbsTile.selectedItem.ownerId, thumbsTile.selectedItem.id));
					navigateToURL(rqt1);
					break;
				case ShellFacade.PERSON_ADD_PREPARE:
					var photo0:Photo = thumbsTile.selectedItem as Photo;
					sendNotification(ShellFacade.PERSON_ADD_PREPARE, photo0.ownerId);
					break;
				case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS:
					var photo1:Photo = thumbsTile.selectedItem as Photo;
					sendNotification(ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS, photo1.ownerId);
					break;
				case 'back':
					break;
				default:
					var photo2:Photo = thumbsTile.selectedItem as Photo;
					sendNotification(e.item.name, photo2);
			}
		}
		
		// photoLoaded
		private function photoLoaded(e:PhotoLoadEvent):void {
			sendNotification(ShellFacade.TILE_PHOTO_LOAD_SUCCESS, e);
		}
		
		private function get thumbsTile():ThumbsTile {
			return viewComponent as ThumbsTile;
		}
		
		private var thumbMenu:Menu;
	}
}